<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@page isELIgnored="false"%>
<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<script type="text/javascript">
	$(document).ready(function(){
		$("#flex1").flexigrid
		(
		{
		url: '<%=request.getContextPath() %>/registroConta.lista.ajax.htm',
		dataType: 'json',
		colModel : [
			{display: 'ID', name : 'registroConta.id', width : 40, sortable : true, align: 'center'},
			{display: 'Name', name : 'registroConta.nome', width : 180, sortable : true, align: 'left'}
			{display: 'Valor Previsto', name : 'registroConta.valorPrevisto', width : 100, sortable : true, align: 'left'}
			{display: 'Prestações', name : 'registroConta.prestacoes', width : 180, sortable : true, align: 'left'}
			{display: 'Vencimento', name : 'registroConta.diaVencimento', width : 180, sortable : true, align: 'left'}
			],
		buttons : [
			{name: 'Add', bclass: 'add', onpress : test},
			{name: 'Delete', bclass: 'delete', onpress : test},
			{name: 'Edit', bclass: 'edit', onpress : test},
			{separator: true},
			{name: 'Select All', bclass : 'add', onpress : test},
			{name: 'DeSelect All', bclass : 'delete', onpress : test},
			{separator: true}
			],
		searchitems : [
			{display: 'ID', name : 'registroConta.id'},
			{display: 'Name', name : 'registroConta.nome', isdefault: true}
			],
		sortname: "registroConta.nome",
		sortorder: "asc",
		usepager: true,
		title: 'registroContas',
		useRp: true,
		rp: 15,
		showTableToggleBtn: true,
		width: 500,
		height: 200
		}
		);   
	
	});
	function test(com,grid)
	{
		if (com=='Select All'){
			$('.bDiv tbody tr',grid).addClass('trSelected');
	    }
		if (com=='DeSelect All'){
			$('.bDiv tbody tr',grid).removeClass('trSelected');
	    }
				
		
		if (com=='Delete'){
			 if($('.trSelected',grid).length>0){
				   if(confirm('Delete ' + $('.trSelected',grid).length + ' items?')){
		            var items = $('.trSelected',grid);
		            var itemlist ='';
		        	for(i=0;i<items.length;i++){
						itemlist+= "{\"registroConta.id\":"+ items[i].id.substr(3)+"}";
						$.ajax({
							   type: "POST",
							   url: '<%=request.getContextPath() %>/registroConta.remove.ajax.htm',
							   data: "registroConta.id="+items[i].id.substr(3),
							   success: function(data){
							   	//alert("Query: "+data.query+" - Total affected rows: "+data.total);
							   
							   },
								error: function (XMLHttpRequest, textStatus, errorThrown) {
								  alert("error "+textStatus + " - " +errorThrown);
								  this; // the options for this ajax request
								}
							 });
						if(i != items.length-1){
							itemlist+=',';
						}
					}
		        	$("#flex1").flexReload();
					}
					} else {
						return false;
					} 
		       }
			if (com=='Add')	{
				window.location = '<%=request.getContextPath() %>/registroConta/novo';
			}
			if(com=='Edit'){
				var id = $('.trSelected',grid)[0].id.substr(3);
				window.location = '<%=request.getContextPath() %>/registroConta/load/'+id;
			}
	}			
</script>
<table id="flex1" style="display:none"></table>
